package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.AllFileRepository;
import com.comum.javahomework420242025.repository.ChanceRepository;
import com.comum.javahomework420242025.repository.TenderRepository;
import com.comum.javahomework420242025.utils.MyFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Optional;

@RestController
public class TenderController {
    @Autowired
    private TenderRepository tenderRepository;
    @Autowired
    private AllFileRepository allFileRepository;
    @Autowired
    private ChanceRepository chanceRepository;
    @PostMapping("/addTender")
    public Result addTender(@RequestBody Tender tender){
        tender.setId(null);
        tender.setStatus("未总结");
        System.out.println(tender);
        tenderRepository.save(tender);
        return Result.success();
    }
    @PostMapping("/getTenders")
    public Result<PageBean<Tender>> getTenders(@RequestBody AskList askList){
        int pageSize = askList.getPageSize();
        int pageNum = askList.getPageNum();
        String str = askList.getStr();
        PageBean<Tender> pb = new PageBean<>();
        Pageable pageable = PageRequest.of(pageNum-1,pageSize);
        Page<Tender> page = tenderRepository.getTenders(str,pageable);
        pb.setTotal(page.getTotalElements());
        pb.setItems(page.getContent());
        return Result.success(pb);
    }
    @PutMapping("/updateTender")
    public Result updateTender(@RequestBody Tender tender){
        System.out.println(tender);
        String status = tender.getStatus();
        Optional<Chance> chancedata = chanceRepository.findById(tender.getChanceid());
        Chance chance = chancedata.get();
        if(status.equals("流标")) chance.setPhase("保留");
        else chance.setPhase("已关闭");
        chanceRepository.save(chance);
        tenderRepository.save(tender);
        return Result.success();
    }

    @DeleteMapping("/deleteTender")
    public Result deleteTender(String id){
        tenderRepository.deleteById(Long.valueOf(id));
        String type = "投标";
        allFileRepository.deleteByElseIdAndType(id,type);
        return Result.success();
    }

    @PostMapping("/uploadTenderFile") //产品管理上传文件
    public Result uploadTenderFile(String elseid,@RequestPart("files") MultipartFile[] files){
        for (MultipartFile file : files) {
            String fileName = MyFileUtil.uploadfile(file);
            AllFile allFile =new AllFile(null,Long.parseLong(elseid),"投标",fileName);
            allFileRepository.save(allFile);
        }
        return Result.success();
    }
    @GetMapping("/getLoadTenderFileName")
    public Result<List<String>>getLoadTenderFileName(String elseid){
        String type = "投标";
        List<String> res = allFileRepository.findfilenames(elseid,type);
        return Result.success(res);
    }
}
